<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    title="选择物料弹窗"
    style="min-width: 1200px"
    draggable
    @close="closeDialog"
  >
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="物料名称">
          <el-input
            v-model="searchParam.materialName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>

        <el-form-item label="物料编号">
          <el-input
            v-model="searchParam.materialCode"
            placeholder="请输入物料编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetHandle"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <div class="formTable">
        <el-table
          ref="multipleTable"
          :data="tableData"
          style="width: 100%"
          max-height="400"
          highlight-current-row
          @current-change="handleCurrentChange1"
          border
        >
          <el-table-column
            label="物料编码"
            prop="materialCode"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />
          <el-table-column
            label="物料名称"
            prop="materialName"
            min-width="100"
            show-overflow-tooltip
            align="left"
          >
            <template #default="scope">
              <span style="text-align: left">
                {{ scope.row.materialName }}</span
              >
            </template>
          </el-table-column>
          <el-table-column
            label="物料类型"
            prop="materialType"
            min-width="100"
            show-overflow-tooltip
            align="left"
          >
            <template #default="scope">
              {{ useDictionary("MaterialType", scope.row.materialType) }}
            </template>
          </el-table-column>
          <el-table-column
            label="物料组"
            prop="materialGroupName"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />
          <el-table-column
            label="短号"
            prop="shortCode"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />
          <el-table-column
            label="图号"
            prop="figureNumber"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />

          <el-table-column
            label="外来编号"
            prop="srcCode"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />
          <el-table-column
            label="国产件标识"
            prop="lcCkd"
            min-width="100"
            show-overflow-tooltip
            align="center"
          >
            <template #default="scope">
              <el-tag type="success" v-if="scope.row.lcCkd === 1">是</el-tag>
              <el-tag type="info" v-else>否</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            label="物料状态"
            prop="materialState"
            min-width="90"
            show-overflow-tooltip
            align="center"
          >
            <template #default="scope">
              {{ useDictionary("MaterialState", scope.row.materialState) }}
            </template>
          </el-table-column>
          <el-table-column
            label="物料规格"
            prop="materialSpecification"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />
          <el-table-column
            label="计量单位"
            prop="unit"
            min-width="90"
            show-overflow-tooltip
            align="center"
          >
            <template #default="scope">
              {{ useDictionary("MeasurementUnit", scope.row.unit) }}
            </template>
          </el-table-column>
          <el-table-column
            label="毛重"
            prop="roughWeight"
            min-width="100"
            show-overflow-tooltip
            align="left"
          />
        </el-table>
        <el-pagination
          :background="true"
          :current-page="pageable.pageNum"
          :page-size="pageable.pageSize"
          :page-sizes="[10, 25, 50, 100]"
          :total="pageable.total"
          layout="total, sizes, prev, pager, next, jumper"
          class="mt-[20px]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit"
          >确 定
        </el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import { ElMessage, ElForm } from "element-plus";
import request from "@/utils/request";
defineOptions({
  name: "BIPRODUCTIONDATAPRODUCTSELECT",
  inheritAttrs: false,
});

const emit = defineEmits(["change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  brekList: {
    type: Object,
    default: () => {
      return {};
    },
  },
  plantId: {
    type: String,
    default: () => {
      return "";
    },
  },
  materialType: {
    type: Number,
    default: () => {
      return null;
    },
  },
});
const multipleTable = ref();

//获取表格数据 api 方法
const requestApi = "/mdm/mdmmaterial/queryByPage";
//获取数据初始化参数
const initParam = {};
const searchParamInit = {
  materialName: "",
  materialCode: "",
};
// 初始化搜索参数数据
// 初始化搜索参数数据
// 是否需要分页
const pagination = true;
// 无需二次数据处理&不传赞数也行
const dataCallBack = (data: any, parentIndex = "") => {
  return data;
};

// 数据请求错误后处理
function requestError() {}
//删除
const deleteParam = {
  api: "",
  data: {},
};

const {
  tableData,
  pageable,
  searchParam,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

const visibleDialog = ref(false);
watch(
  () => props.visible,
  async (newVal: boolean) => {
    visibleDialog.value = newVal;
    searchParam.value.materialType = props.materialType;
    searchParam.value.plantId = props.plantId;
    getTableList();
  }
);

// 选择高亮
const currentRow = ref();
const handleCurrentChange1 = (val: any) => {
  currentRow.value = val;
};

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  reset();
}
/** email消息发送记录保存提交 */
function handleSubmit() {
  if (currentRow.value) {
    emit("change-visible", false, currentRow.value);
  } else {
    return ElMessage.warning("请选择物料");
  }
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

const resetHandle = () => {
  Object.assign(searchParam.value, searchParamInit);
  searchParam.value.materialType = props.materialType;
  searchParam.value.plantId = props.plantId;
  getTableList();
};
</script>

<style scoped lang="scss">
:deep(.el-table th .cell) {
  color: black;
  font-weight: 700;
}
.progress {
  color: red;
}
.success {
  color: rgb(100, 207, 86);
}
</style>
